C. Lab Practive
//1. to enter name, roll_no. and marks of 10 students and
store
them in the file :
#include (stdio.h)
main()
{ FILE *f; //file pointer
char name[10];
int roll_no,L;
float marks;
f=fopen("record.txt","w");
//creating file in write mode
for(L=1;L <=10;L++)
{ printf("Enter roll no");
scanf("%d",&roll_no);
printf("Enter
name");
scanf("%s",&name);
printf("Enter marks");
scanf("%f",&marks);
fprintf(f,"%d
\t %s\t %f\n",roll_no,name,marks); //write data into file; }
fclose(f);
//closing
file; }
//2. to store roll, name, age, gender into "info.txt" fiLe:
#include stdio.h>
#include string.h>
#include conio.h>
main(){
FILE *fp;
int roll, age, p, n;
char name[10], sex[10], ch='Y';
fp = fopen ("info.txt","a");
while (ch == 'Y' || ch == 'y')
{ printf ("Enter roll");
scanf ("%d", &roll);
printf ("Enter Name");
scanf ("%s", &name);
printf ("Enter Age");
scanf ("%d", &age);
printf ("Enter Gender");
scanf ("%s", &sex);
fprintf (fp,"%d\t %s\t %d\t %s\n", roll, name, age, sex);
printf ("Press Y for nest record");
ch = getche ();
}
fclose (fp);
}
//3. to
display contents: roll,name,age,sex from "infoitxt" fiLe:
#include (stdio.h>
main(){
FILE *fp;
int roll,age,p,n;
char name[10],sex[10];
fp=fopen("info.txt","r");
while((fscanf(fp,"%d\t %s\t %d\t %s\n",&roll,name,&age,sex))!=EOF)
{
printf("Roll:%d\t Name: %s\t Age: %d\t %s\n",roll,name,age,sex);
}
fclose(fp);
}
//this program reads contents as defined format of data type and displays
accordingly till the end of content in the file:
//4. to dispLoy contents from "infoitxt" fiLe:
#include (stdio.h>
main(){
FILE *fp;
char ch;
fp=fopen("info.txt","r");
while((ch=getc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
}
//this program reads as all characterwise and displays accordingly till the
end of content in the file:
/*5. to take a number and check whether it is positive,
negative or zero using user-defined function.*/
#include (stdio.h>
char check (int);//declaration of function
main()
{ int n;
printf ("Enter a number");
scanf ("%d", &n);
check(n); } //calling function
char check (int n) // definition part of function
{
if (n>0){
printf ("The number %d is Positive",n);
}
else if (n<0){
printf("The number %d is Negative",n); }
else{ printf("The number %d is Zero",n); } }
/*6. a program to add record on file "Record.dat" under the
fields: ID, Name, Post and Salary of nth employees.*/
#include (stdio.h>
#include (conio.h>
main(){
FILE *fp;
int ID, sal;
char name[10], post[10], ch='Y';
fp = fopen ("Record.dat","a"); //append mode is to add records
while (ch == 'Y' || ch == 'y')
{ printf ("Enter ID");
scanf ("%d", &ID);
printf ("Enter Name");
scanf ("%s", &name);
printf ("Enter Post");
scanf ("%s", &post);
printf ("Enter Gender");
scanf ("%d", &sal);
fprintf (fp,"%d\t %s\t %s\t %d\n", ID, name, post, sal);
printf("Press Y for nest record");
ch = getche ();
}
fclose (fp);
}
/*7. program to take ID, Name, Post and Salary of nth
employees
and print the record of 5 higher salary receivers.*/
#include (stdio.h>
struct emplyees{
int ID;
char Name[10],Post[10];
float sal;
}e[100],holder;
main(){
int n, p, p1;
printf("Enter the number of employees");
scanf("%d",&n);
for(p=0; p < n; p++){
printf("Enter ID");
scanf("%d", &e[p].ID);
printf("Enter
Name");
scanf("%s", &e[p].Name);
printf("Enter Post");
scanf("%s", &e[p].Post);
printf("Enter
Salary");
scanf("%f", &e[p].sal); }
for(p=0; p < n; p++)
{ for(p1=0; p1 <
n;
p1++) {
if(e[p].sal>e[p1].sal)
{holder=e[p];
e[p]=e[p1];
e[p1]=holder;
}
}
}
printf("The 5 higher salary receivers:\n");
for(p=0;p <5;p++){
printf("ID: %d\n Name: %s\n",e[p].ID,e[p].Name);
printf(" Post:%s\n
Salry: %.2f\n",e[p].Post,e[p].sal); }
}
/* program to take roll, name, age of nth numbers of
students and print by name alphabetically.*/
#include (stdio.h>
#include (string.h>
struct students{
int roll,age;
char Name[10];
}e[100],holder;
main(){
int n, p, p1;
printf("Enter the number of students");
scanf("%d",&n);
for(p=0;p < n; p++){
printf("Enter Roll No.");
scanf("%d",&e[p].roll);
printf("Enter Name");
scanf("%s",&e[p].Name);
printf("Enter Age");
scanf("%d",&e[p].age); }
for(p=0; p < n; p++)
{ for(p1=0; p1 < n; p1++)
{ if((strcmp(e[p].Name,e[p1].Name))<0)
{ holder=e[p];
e[p]=e[p1];
e[p1]=holder; } } }
printf("The list in alphabetical order:\n");
for(p=0; p < n; p++){
printf("Roll: %d\n Name: %s\n",e[p].roll,e[p].Name);
printf(" Age:%d\n",e[p].age); }
}